//rem布局屏幕适配
(function (doc, win) {
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function () {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                if(clientWidth>=640){
                    docEl.style.fontSize = '100px';
                }else{
                    docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
                }
            };

        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(document, window);


window.onload=function(){
	//播放暂停按钮切换
	var audioControl = document.getElementById('audio-control');
	var audioMedia = document.getElementById('media');
	audioControl.addEventListener('click',function(){
		if(audioMedia.paused){
			audioMedia.play();
			audioControl.className='audio-pause';
		}else{
			audioMedia.pause();
			audioControl.className='audio-star';
		}
	})
	
	//进度条拖动
	var progressBtn = document.getElementById('progress-bar-btn');
	var progressBox = document.getElementById('progress-bar-box');
	var progressActive = document.getElementById('progress-bar-active');
	var o = document.documentElement;
	progressBtn.onmousedown=function(e){
		var e = e||window.event;
		var leftval = e.clientX-this.offsetLeft;
		document.onmousemove=function(ev){
			var ev = window.event;
			ev.preventDefault();
			var barleft = ev.clientX-leftval;
			if(barleft<0){
				barleft=0;
			}else if(barleft>progressBox.offsetWidth-progressBtn.offsetWidth){
				barleft=progressBox.offsetWidth-progressBtn.offsetWidth;
			}
			progressBtn.style.marginLeft=barleft/parseFloat(o.style.fontSize)+'rem';	//rem单位换算,适配不同尺寸屏幕;
			progressActive.style.width=barleft/progressBox.offsetWidth*100+'%';
			//拖动快进
			audioMedia.currentTime=barleft/progressBox.offsetWidth*audioMedia.duration;
		}
	}
	//鼠标抬起释放内存,取消事件
	document.onmouseup=function(){
		document.onmousemove=null;
	}
	
	
	//移动端手势操作
	var touchLeftval;
	progressBtn.addEventListener('touchstart',function(e){
		var touch = e.touches;
		touchLeftval=touch[0].clientX-this.offsetLeft;
	})
	progressBtn.addEventListener('touchmove',function(e){
		e.preventDefault();
		var touch = e.touches;
		var barleft = touch[0].clientX-touchLeftval;
			if(barleft<0){
				barleft=0;
			}else if(barleft>progressBox.offsetWidth-progressBtn.offsetWidth){
				barleft=progressBox.offsetWidth-progressBtn.offsetWidth;
			}
			progressBtn.style.marginLeft=barleft/parseFloat(o.style.fontSize)+'rem';	//rem单位换算,适配不同尺寸屏幕;
			progressActive.style.width=barleft/progressBox.offsetWidth*100+'%';
			//拖动快进
			audioMedia.currentTime=barleft/progressBox.offsetWidth*audioMedia.duration;
	})
	
	
	//进度条点击事件
	progressBox.onclick=function(e){
		var e = e||window.event;
		var offsetX = e.offsetX;
		if(offsetX<0){
			progressBtn.style.marginLeft=0+'rem';
			rogressActive.style.width=0+'%';
		}else if(offsetX>progressBox.offsetWidth-progressBtn.offsetWidth){
			offsetX=progressBox.offsetWidth-progressBtn.offsetWidth;
		}
		progressBtn.style.marginLeft=offsetX/parseFloat(o.style.fontSize)+'rem';
		progressActive.style.width=offsetX/progressBox.offsetWidth*100+'%';
		//点击快进
		audioMedia.currentTime=offsetX/progressBox.offsetWidth*audioMedia.duration;
	}
	progressActive.onclick=function(e){
		var e = e||window.event;
		var offsetX = e.offsetX;
		if(offsetX<0){
			progressBtn.style.marginLeft=0+'rem';
			rogressActive.style.width=0+'%';
		}else if(offsetX>progressBox.offsetWidth-progressBtn.offsetWidth){
			offsetX=progressBox.offsetWidth-progressBtn.offsetWidth;
		}
		progressBtn.style.marginLeft=offsetX/parseFloat(o.style.fontSize)+'rem';
		progressActive.style.width=offsetX/progressBox.offsetWidth*100+'%';
		//点击快进
		audioMedia.currentTime=offsetX/progressBox.offsetWidth*audioMedia.duration;
	}
	
	//获取音频总时长
	var totalTime = document.getElementById('total-time');
	var audioCurrentTime = document.getElementById('current-time');
	audioMedia.addEventListener('canplay',function(){
		totalTime.innerHTML=formatTime(audioMedia.duration);
	})
	audioMedia.addEventListener('timeupdate',function(){
		var currentTime = audioMedia.currentTime;
		audioCurrentTime.innerHTML=formatTime(currentTime);
		progressBtn.style.marginLeft=currentTime/audioMedia.duration*progressBox.offsetWidth/parseFloat(o.style.fontSize)+'rem';
		progressActive.style.width=currentTime/audioMedia.duration*100+'%';
	})
	
	//播放完后触发的事件
	audioMedia.addEventListener('ended',function(){
		audioMedia.pause();
		audioControl.className='audio-star';
		audioMedia.currentTime=0;
	})
}

//音频文件时间转换方法
function formatTime(time) {
  var min = Math.floor(time / 60);
  var sec = Math.floor(time % 60);
  return min + ':' + (sec < 10 ? '0' + sec : sec);
}

function msg(){
	window.location.href='./msg.html';
}

/*
 * 1.火狐IE不支持播放mp3,音频请用ogg格式的;
 * 
 */